package com.cw.jeeyt.controller.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.service.bo.api.ApiFacadeDocBo;
import com.cw.jeeyt.service.bo.api.ApiFacadeDocDto;
import com.cw.jeeyt.service.intf.api.ApiFacadeDocService;

/**
 * api接口文档控制层
 *
  * @author chenw
 * @version 1.0.0
 */
@Controller
@RequestMapping("/api_doc")
public class ApiFacadeDocPublicController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(ApiFacadeDocPublicController.class);
    @Resource
    private ApiFacadeDocService apiFacadeDocService;

    /**
     * 跳转到api接口文档查询页面
     *
     * @return
     */
    @RequestMapping(value = "/api")
    public ModelAndView searchDoc() {

        ModelAndView mv = new ModelAndView("api/apiFacadeDoc_search");
        return mv;
    }

    /**
     * 跳转到api接口文档查询页面
     *
     * @param docId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doc_detail/{docId}", method = RequestMethod.GET)
    public ModelAndView docDetail(@PathVariable("docId") Long docId) throws Exception {

        ModelAndView mv = new ModelAndView("api/apiFacadeDoc");
        ApiFacadeDocBo apiFacadeDoc = apiFacadeDocService.getApiFacadeDocById(docId);
        String apiFacadeMavenConfig = apiFacadeDoc.getApiFacadeMavenConfig();
        if (!StringUtils.isEmpty(apiFacadeMavenConfig)) {
            apiFacadeMavenConfig = apiFacadeMavenConfig.replace("<", "&lt;")
                .replace(">", "&gt;");
            apiFacadeDoc.setApiFacadeMavenConfig(apiFacadeMavenConfig);
        }
        mv.addObject("apiFacadeDoc", apiFacadeDoc);
        return mv;
    }

    /**
     * 下载api接口文档
     *
     * @param docId
     * @return
     */
    @RequestMapping(value = "/download_doc")
    public void downloadDoc(HttpServletRequest request, HttpServletResponse response, Long docId) {
        try {
            apiFacadeDocService.downloadDoc(docId, request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询api接口文档
     *
     * @param requestData
     * @throws Exception
     */
    @RequestMapping(value = "/search_result")
    @ResponseBody
    public JsonResultDto searchResult(String requestData) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            List<ApiFacadeDocBo> apiDocList = apiFacadeDocService.searchResult(requestData);
            jsonResultDto.setSuccess(true);
            Map<String, Object> map = new HashMap<>();
            map.put("apiDocList", apiDocList);
            jsonResultDto.setResultData(map);
        } catch (Exception e) {
            e.printStackTrace();

            jsonResultDto.setMsg("查询api接口文档失败：" + e.getMessage());
            logger.error("查询api接口文档失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 初始化接口数据
     *
     * @throws Exception
     */
    @RequestMapping(value = "/init_data")
    @ResponseBody
    public JsonResultDto initData() {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            jsonResultDto = apiFacadeDocService.initData();
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("查询api接口文档失败：" + e.getMessage());
            logger.error("查询api接口文档失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 加载项目接口数据
     *
     * @throws Exception
     */
    @RequestMapping(value = "/load_data/{projectCode}", method = RequestMethod.POST)
    public JsonResultDto loadData(@PathVariable("projectCode") String projectCode) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            List<ApiFacadeDocDto> apiList = apiFacadeDocService.queryListByProjectCode(projectCode);
            Map<String, Object> map = new HashMap<>();
            map.put("apiDocList", apiList);
            jsonResultDto.setResultData(map);
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("查询api接口文档失败：" + e.getMessage());
            logger.error("查询api接口文档失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 跳转到api接口文档预览页面
     *
     * @param docId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doc_view/{docId}", method = RequestMethod.GET)
    public String docView(@PathVariable("docId") Long docId) throws Exception {
        Map<String, Object> fileNameMap = apiFacadeDocService.generateReviewFile(docId);
        String swfFileName = (String) fileNameMap.get("swfFileName");
        return "redirect:/pdf_factory/view_pdf/" + swfFileName;
    }
}
